#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@author: KevinQi
@file: __init__.py
@time: 2023/12/26 18:28
@desc:
"""
from backend.workjob.utils.base_response import SuccessResponse, ErrorResponse


def checkIfNotPersisnt(handlerSelf, value):
    if not value:
        return handlerSelf.write(ErrorResponse(msg='必要的参数不存在', code=500))
    else:
        return value


def token_check(handler_class):
    def wrap_execute(handler_execute):
        def require_auth(handler, kwargs):
            """
            :param handler:
            :param kwargs:
            :return:
            # I've pulled this out just for clarity, but you could stick
            # it in _execute if you wanted.  It returns True iff
            # credentials were provided.  (The end of this function might
            # be a good place to see if you like their username and
            # password.)
            """
            if 'token' not in handler.session:
                handler.set_status(401)
                handler._transforms = []
                handler.write('session expired, please login again')
                handler.finish()
                return False

            return True

        def _execute(self, transforms, *args, **kwargs):
            """
            :param self:
            :param transforms:
            :param args:
            :param kwargs:
            :return:
            # Since we're going to attach this to a RequestHandler class,
            # the first argument will wind up being a reference to an
            # instance of that class.
            """
            if not require_auth(self, kwargs):
                return False
            return handler_execute(self, transforms, *args, **kwargs)

        return _execute

    handler_class._execute = wrap_execute(handler_class._execute)
    return handler_class
